package org.se.scut.pinterest.mina.codec.filte.coder.decoder;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;
import org.se.scut.pinterest.mina.codec.coder.cxt.BaseContext;
import org.se.scut.pinterest.mina.codec.coder.cxt.PictureUploadContext;
import org.se.scut.pinterest.mina.codec.msg.PictureUploadMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PictureUploadMessageDecoder extends BaseMessageDecoder  {

	
	private static final Logger LOGGER = LoggerFactory.getLogger(PictureUploadMessageDecoder.class);
	
	public PictureUploadMessageDecoder(){
		super(new AttributeKey(
			PictureUploadMessageDecoder.class, "context"), "pu");
	}

	@Override
	public MessageDecoderResult decode(IoSession session, IoBuffer in,
			ProtocolDecoderOutput out) throws Exception {
		PictureUploadContext context = (PictureUploadContext)getContext(session);
		int matchCount = context.getMatchCount();
		int lineNumber = context.getLineNumber();
		String symbol = context.getSymbol();
		String fileName = context.getFileName();
		String overView = context.getOverView();
		String userID = context.getUserID();
		String pictureTypeID = context.getPictureTypeID();
		String ablumID = context.getAblumID();
		String fileLength = context.getFileLength();
		IoBuffer buffer = context.getInnerBuffer();
		
		while (in.hasRemaining()) {
			byte b = in.get();
			matchCount++;
			buffer.put(b);
			if (lineNumber < 7 && b == 10) {
				if (lineNumber == 0) {
					buffer.flip();
					symbol = buffer.getString(matchCount, DECODER);
					symbol = symbol.substring(0, symbol.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setSymbol(symbol);
				}
				if (lineNumber == 1) {
					buffer.flip();
					fileName = buffer.getString(matchCount, DECODER);
					fileName = fileName.substring(0, fileName.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setFileName(fileName);
				}
				if (lineNumber == 2) {
					buffer.flip();
					overView = buffer.getString(matchCount, DECODER);
					overView = overView.substring(0, overView.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setOverView(overView);
				}
				if (lineNumber == 3) {
					buffer.flip();
					userID = buffer.getString(matchCount, DECODER);
					userID = userID.substring(0, userID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setUserID(userID);
				}
				if (lineNumber == 4) {
					buffer.flip();
					pictureTypeID = buffer.getString(matchCount, DECODER);
					pictureTypeID = pictureTypeID.substring(0, pictureTypeID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setPictureTypeID(pictureTypeID);
				}
				
				if (lineNumber == 5) {
					buffer.flip();
					ablumID = buffer.getString(matchCount, DECODER);
					ablumID = ablumID.substring(0, ablumID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setAblumID(ablumID);
				}
				if (lineNumber == 6) {
					buffer.flip();
					fileLength = buffer.getString(matchCount, DECODER);
					fileLength = fileLength.substring(0, fileLength.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setFileLength(fileLength);
				}
				lineNumber++;
			}else if (lineNumber == 7) {
				if (matchCount == Integer.parseInt(fileLength)) {
					buffer.flip();
					//String contextStr = buffer.getString(matchCount , DECODER);
					InputStream inStream = buffer.asInputStream();
					byte[] file = new byte[Integer.parseInt(fileLength)];
					inStream.read(file);
					//content = contextStr.getBytes(Charset.forName("UTF-8"));
					context.setContent(file);
					context.setMatchCount(matchCount);
					context.setLineNumber(lineNumber);
					break;
				}
			}
			context.setMatchCount(matchCount);
			context.setLineNumber(lineNumber);
		}
		if (context.getLineNumber() == 7 &&
				Integer.parseInt(context.getFileLength()) == context.getMatchCount()) {
			PictureUploadMessage msg = new PictureUploadMessage();
			msg.setSymbol(context.getSymbol());
			msg.setFileName(context.getFileName());
			msg.setOverView(context.getOverView());
			msg.setUserID(Integer.parseInt(context.getUserID()));
			msg.setPictureTypeID(Integer.parseInt(context.getPictureTypeID()));
			msg.setAblumID(Integer.parseInt(context.getAblumID()));
			msg.setFileLength(Integer.parseInt(context.getFileLength()));
			msg.setContent(context.getContent());
			out.write(msg);
			context.resetData();
			return MessageDecoderResult.OK;
		}else {
			return MessageDecoderResult.NEED_DATA;
		}
		
	}

	@Override
	public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
			throws Exception {

	}
	
	@Override
	public BaseContext getContext(IoSession session)
	{
		PictureUploadContext context = (PictureUploadContext)session.getAttribute(CONTEXT_KEY);
		if (context == null) {
			context = new  PictureUploadContext();
			session.setAttribute(CONTEXT_KEY, context);
		}
		return context;
	}
	
	
	
	
}
